
global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/offshoring.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {
*import the country list to switch from three to two digit later on
import excel "${mow_data_raw}/countries/country_3_code.xlsx", clear cellrange(B2:C249)

ren C country
tempfile countries
save `countries', replace

*import the files generated by matlab
*first the two files with templates
import excel "${dataset_dir}/import/agg_foreign_shares_modified_selina.xlsx", clear  sheet("post") cellrange(B3:D43)
drop C

ren B country
ren D index

tempfile template1
save `template1', replace

import excel "${dataset_dir}/import/WIOD_2016/agg_foreign_shares_modified_selina_WIOD16.xlsx", clear  sheet("post") cellrange(B3:C46)

ren B country
ren C index

tempfile template2
save `template2', replace

*now the actual values
*first, so we can merge afterwards, the supplementary data for switzerland
import excel "${dataset_dir}/import/foreign shares mfg & non-mfg WIOD 2016.xlsx", clear sheet("share") cellrange(A1:Q89) firstrow
keep if B == "_2"
drop B
*keep only switzerland
keep if A == "_7"
replace A = subinstr(A, "_", "", .)
ren A index
*index needs to be byte
destring index, replace

mmerge index using `template2', unmatched(master)
drop _m

tempfile CHE
save `CHE', replace

*do the main data
import excel "${dataset_dir}/import/foreign shares mfg & non-mfg.xlsx", clear sheet("share") cellrange(A1:S83) firstrow
keep if B == "_2"
drop B
replace A = subinstr(A, "_", "", .)
ren A index
*index needs to be byte
destring index, replace

mmerge index using `template1'
drop _m

append using `CHE', force
sort country
drop index

*merge in 2 digit codes
mmerge country using `countries', unmatched(master)
*romania is somehow not matched, ROW we do not need
replace B = "RO" if country == "ROM"
drop if B == ""
drop _m country
ren B country
*careful: this sort is needed, otherwise australia and austria switch data
sort country

*new need to bring the data in a country-colum year-row format
levelsof country, local(countries)
local nvar = r(r)

xpose, clear varname
forval i = 1/`nvar' {
    local nextvar: word `i' of `countries'
    rename v`i' `nextvar'
}

drop if _varname == "country"
gen year = real(subinstr(_varname, "_", "", .))
drop _varname
order year

*rename
ds
foreach var of varlist * {
    if "`var'" == "year" {
        continue
    }
    ren `var' offshoring_`var'
}
sort year
drop if year > 2009

*Switzerland is missing the first 4 years
sort year, stable
gsort -year

* Replace missing values for offshoring_CH with the value from the next year
replace offshoring_CH = offshoring_CH[_n-1] if missing(offshoring_CH)
sort year, stable

*now format the data
ds
foreach var of varlist * {
    recast float `var'
    format `var' %10.0g
}




save ${dataset_dir}/import/offshoring_data.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat